//
//  RootTabBarVC.swift
//  easeIM-swift
//
//  Created by yangjian on 2022/7/13.
//

import UIKit

import RxSwift

class EMRootTabBarVC: UITabBarController {

    //未读消息数相关
    var conversationItem : UITabBarItem?
    var bag = DisposeBag()
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        config()
        
        
    }
    
    func config(){
        self.view.backgroundColor = UIColor.white
        self.addChildVC()
        //未读消息数
        configUnReadMessageCount()
    }
    
    
    func addChildVC(){
        
        let conversationVC = EMConversationVC()
        let conversationNC = UINavigationController(rootViewController: conversationVC)
        conversationVC.navigationItem.title = "会话"
        conversationNC.tabBarItem.title = "会话"


        
        let contactVC = EMContactVC()
        let contactNC = UINavigationController(rootViewController: contactVC)
        contactNC.tabBarItem.title = "通讯录"

        let mineVC = EMMineVC()
        let mineNC = UINavigationController(rootViewController: mineVC)
        mineNC.tabBarItem.title = "我的"

        self.viewControllers = [conversationNC,contactNC,mineNC]
        
        conversationItem = conversationNC.tabBarItem;
    }
    
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}

extension EMRootTabBarVC {
    //处理未读消息数
    func configUnReadMessageCount(){
        EMIMHelper.share.unreadSubject.subscribe { unreadCount in
            if unreadCount == 0 {
                self.conversationItem?.badgeValue = nil;
            }else if unreadCount > 99 {
                self.conversationItem?.badgeValue = "99+";
            }else{
                self.conversationItem?.badgeValue = "\(unreadCount)"
            }
        }.disposed(by: bag)
    }
}
